<?php

/**
 * uniBoard ACP Custom BBCodes List
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_bbcodes{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] =  $appCore -> lang['delete_item_sure'];
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_half',
				$appCore -> lang['acp_bbcodes_list_name']
			),
			array(
				'left_float width_quarter',
				$appCore -> lang['acp_bbcodes_list_code']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			)
		));
			
		// Hide or show BBCode?
		if ( $appCore -> _REQ( 'do') == 'show' || $appCore -> _REQ( 'do') == 'hide')
		{
			// Make Api
			$bbcode_api = new api_bbcode( $appCore, $appCore -> _REQ( 'bbcode'));
			
			// Loaded?
			if ( $bbcode_api -> data_loaded)
			{
				// Set its type
				$bbcode_api -> setBBCodeInEditor( $appCore -> _REQ( 'do') == 'show');
				
				// Save api
				$bbcode_api -> saveData();
				
				// Set Log
				$appCore -> logAdmin(($appCore -> _REQ( 'do') == 'show' ? 'log_admin_bbcode_shown' : 'log_admin_bbcode_hidden'), $bbcode_api -> bbcode_name);
				
				// Draw Message
				$appCore -> output -> message($appCore -> lang[($appCore -> _REQ( 'do') == 'show' ? 'acp_bbcode_shown' : 'acp_bbcode_hidden')], $this -> appCore -> ifString(htmlspecialchars($bbcode_api -> bbcode_name)));
			}
			else
			{
				// Error
				$appCore -> output -> error( $appCore -> lang['acp_bbcode_not_found'], $appCore -> lang['acp_bbcodes_custom']);
			}
		}
		
		// Select System BBCodes
		$appCore -> db -> query( 'SELECT bbcode_id, bbcode_system, bbcode_in_editor, bbcode_name, bbcode_tag
		FROM ' . DB_PREFIX . 'bbcodes
		WHERE bbcode_system = \'0\'');
		
		// Parse result
		while ( $bbcode_result = $appCore -> db -> fetch_array())
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_half">
					<a href="' . $appCore -> adminLink('act=bbcodes&do=' . ($bbcode_result[2] ? 'hide' : 'show') . '&bbcode=' . $bbcode_result[0]) . '">' . $appCore -> output -> drawImage( 'state_' . $bbcode_result[2], $appCore -> lang['acp_bbcodes_list_in_editor_' . $bbcode_result[2]]) . '</a> <b>' . $this -> appCore -> ifString( $bbcode_result[3]) . '</b>
				</div>
				<div class="cell left_border left_float width_quarter">
					<b>' . htmlspecialchars( $bbcode_result[4]) . '</b>
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=edit_bbcode&do=edit&bbcode=' . $bbcode_result[0]) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['edit']) . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_bbcode&do=delete&bbcode=' . $bbcode_result[0]) . '" class="delete_confirm">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>');
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New Group Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_bbcode'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['acp_new_bbcode_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['acp_new_bbcode_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_bbcodes_custom'], array( $appCore -> adminLink( 'act=bbcodes') => $appCore -> lang['acp_bbcodes_custom']), ob_get_clean());
				
	}

}